﻿using System.Web.Mvc;
using RecruitmentAgency.Models.DataTypes;

namespace RecruitmentAgency.Models.Validation
{
    /// <summary>
    /// Validates all JobProvider related data
    /// </summary>
    public class JobProviderValidator : BaseValidator
    {       
        private readonly ModelStateDictionary modelState;
        
        /// <summary>
        /// Initialises a new RecruitmentAgency.Validation.JobProvider instance
        /// </summary>        
        public JobProviderValidator(ModelStateDictionary modelState, string selectMessage): base(selectMessage)
        {
            this.modelState = modelState;
        }

        /// <summary>
        /// Validate the Job Provider data
        /// </summary>
        /// <param name="jobProvider">The instance of the JobProvider containing all the data relative to the job provider</param>
        /// <returns>Returns true if the validation is successfully</returns>
        public bool ValidateJobProvider(JobProvider jobProvider)
        {
            if (jobProvider.IsNull) return false;
            if (IsEmpty(jobProvider.FirstName)) modelState.AddModelError("FirstName", "This field is required.");
            if (IsEmpty(jobProvider.LastName)) modelState.AddModelError("LastName", "This field is required.");
            if (IsEmpty(jobProvider.CompanyName)) modelState.AddModelError("CompanyName", "This field is required.");
            if (IsEmpty(jobProvider.RegistrationNumber)) modelState.AddModelError("RegistrationNumber", "This field is required.");
            if (IsEmpty(jobProvider.AddressLine1)) modelState.AddModelError("AddressLine1", "This field is required.");
            if (IsEmpty(jobProvider.Town)) modelState.AddModelError("Town", "This field is required.");
            if (IsEmpty(jobProvider.PostCode)) modelState.AddModelError("PostCode", "This field is required.");
            if (IsEmpty(jobProvider.Phone)) modelState.AddModelError("Phone", "This field is required.");
            if (IsEmpty(jobProvider.Email)) modelState.AddModelError("Email", "This field is required.");
            if (IsEmpty(jobProvider.WebsiteUrl)) modelState.AddModelError("WebsiteUrl", "This field is required.");
            if (IsEmpty(jobProvider.UserName)) modelState.AddModelError("UserName", "This field is required.");
            if (ValidateSelection(jobProvider.Title)) modelState.AddModelError("Title", "This field is required.");
            if (ValidateSelection(jobProvider.Sector)) modelState.AddModelError("Sector", "This field is required.");
            if (ValidateEmail(jobProvider.Email)) modelState.AddModelError("Email", "Please enter a valid e-mail address.");
            if (ValidateUrl(jobProvider.WebsiteUrl)) modelState.AddModelError("WebsiteUrl", "Please enter a valid website URL.");
            return modelState.IsValid;
        }
    }
}